package com.example.config;


import com.example.task.MyJob;
import com.example.task.WebPageCrawlerJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class QuartzConfig {






    // 定义 JobDetail
    @Bean
    public JobDetail jobDetail() {
        return JobBuilder.newJob().ofType(WebPageCrawlerJob.class)
                .storeDurably()
                .withIdentity("webPageCrawlerJob")
                .withDescription("Invoke WebPageCrawlerJob")
                .build();
    }

    // 定义 Trigger
    @Bean
    public Trigger trigger(JobDetail job) {
        return TriggerBuilder.newTrigger()
                .forJob(job)
                .withIdentity("webPageCrawlerTrigger")
                .withDescription("SimpleTrigger")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(500)  // 每隔10秒执行一次
                        .repeatForever())
                .build();
    }

    // 启动 Scheduler
    @Bean
    public Scheduler scheduler(Trigger trigger, JobDetail job, SchedulerFactory schedulerFactory) throws SchedulerException {
        Scheduler scheduler = schedulerFactory.getScheduler();
        scheduler.scheduleJob(job, trigger);
        scheduler.start();
        return scheduler;
    }

    // 定义 SchedulerFactory
    @Bean
    public SchedulerFactory schedulerFactory() {
        return new StdSchedulerFactory();
    }













}